package com.polaris.live.common.ext

import com.polaris.live.common.util.AppCodeUtils.isAR


fun String.clear(input: String, charsToRemove: List<Char>): String {
    val regex = charsToRemove.joinToString("|").toRegex()
    return input.replace(regex, "")
}

fun String.removeParen(): String {
    val regex = "\\s*\\(.*?\\)".toRegex()
    return this.replace(regex, "")
}

//除掉所有反斜杠
fun String.removeBackslashes(): String {
    return this.replace("\\", "")
}

//json转map
fun String.toMutableMap(): MutableMap<String, String> {
    val map = mutableMapOf<String, String>()
    val temp = this.replace("[{}]".toRegex(), "")
    val replace = temp.replace("\"", "")
    val keyValue = replace.split(",")
    keyValue.forEach {
        val pairs = it.split(":")
        if (pairs.size == 2) {
            map[pairs[0]] = pairs[1]
        }
    }
    return map
}


val digitsMap: Map<Char, Char> by lazy {
    mapOf(
        '٠' to '0',
        '١' to '1',
        '٢' to '2',
        '٣' to '3',
        '٤' to '4',
        '٥' to '5',
        '٦' to '6',
        '٧' to '7',
        '٨' to '8',
        '٩' to '9'
    )
}

fun String.convertArabicDigitsToNormal(): String {
    if (this.isBlank() || !isAR()) {
        return this
    }

    val convertedDigits = StringBuilder()
    for (char in this) {
        val convertedChar = digitsMap[char]
        if (convertedChar != null) {
            convertedDigits.append(convertedChar)
        } else {
            convertedDigits.append(char)
        }
    }

    return convertedDigits.toString()
}